﻿using System;
using System.Collections.ObjectModel;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Threading;
using YAWD.BL.Services;
using YAWD.Properties;
using YAWD.Utils;
using YAWD.Windows;

namespace YAWD
{
    public partial class App
    {
        private Mutex _mutex;
        private DispatcherTimer _timer = new DispatcherTimer();
        private ResourceDictionary _loadedLanguageResourceDictionary;
        public string Ip { get; set; }

        public DispatcherTimer Timer
        {
            get
            {
                return _timer;
            }
            set
            {
                _timer = value;
            }
        }


        private Collection<ResourceDictionary> MergedDictionaries
        {
            get { return Resources.MergedDictionaries; }
        }
        public ResourceDictionary LoadedLanguageResourceDictionary
        {
            get { return _loadedLanguageResourceDictionary; }
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            try
            {
                bool isNewInstance;
                _mutex = new Mutex(true, "YAWD.Singleton", out isNewInstance);
                if (!isNewInstance)
                    Current.Shutdown();

                Ip = string.Empty;

                base.OnStartup(e);
            }
            catch (Exception ex)
            {
                CatchException(ex);
            }
        }

        private void Application_Startup(object sender, StartupEventArgs e)
        {
            try
            {
                Settings settings = Settings.Default;
                _timer.Tick += ChangeWallPaper;

                Ip = DatabaseServices.GetIp();

                foreach (ResourceDictionary item in MergedDictionaries)
                {
                    if (item.Source != null && item.Source.OriginalString.Contains(@"Languages\"))
                        _loadedLanguageResourceDictionary = item;
                }

                if (settings.RefreshIntervall > 0)
                {
                    _timer.Interval = Util.GetDuration(settings.RefreshIntervall, (Units)Enum.Parse(typeof(Units), settings.RefreshUnit, false));
                    _timer.Start();
                }
            }
            catch (Exception ex)
            {
                CatchException(ex);
            }

        }

        public void ApplyLanguage(Uri objLanguageDictionaryUri)
        {
            if (string.IsNullOrEmpty(objLanguageDictionaryUri.OriginalString) == false)
            {
                ResourceDictionary objNewLanguageDictionary =
                    (ResourceDictionary)(LoadComponent(objLanguageDictionaryUri));

                if (objNewLanguageDictionary != null)
                {
                    MergedDictionaries.Remove(_loadedLanguageResourceDictionary);
                    _loadedLanguageResourceDictionary = objNewLanguageDictionary;
                    MergedDictionaries.Add(objNewLanguageDictionary);
                }
            }
        }
        void ChangeWallPaper(object sender, EventArgs e)
        {
            try
            {
                bool changed = false;
                while (changed == false)
                    changed = Util.ChangeWallPaper(WallPaperServices.GetRandomWallpaper());
            }
            catch (Exception ex)
            {
                CatchException(ex);
            }
        }
        private void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
        {
            CatchException(e.Exception);
            e.Handled = true;
        }
        private static void CatchException(Exception ex)
        {
            Task.Factory.StartNew(() => Util.LogException(ex));
            new MessageBoxYesNo(ex.Message, false, true).ShowDialog();

        }
    }
}
